package com.ssi.mrms.action.room;

import static com.opensymphony.xwork2.Action.SUCCESS;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Namespaces;
import org.apache.struts2.convention.annotation.Result;

import com.ssi.mrms.action.BaseAction;
import com.ssi.mrms.model.Msg;
import com.ssi.mrms.model.RMaccrModel;
import com.ssi.mrms.model.Msg.MsgType;
import com.ssi.mrms.model.RMrmlstModel;
import com.ssi.mrms.service.RoomProfileService;
import com.ssi.mrms.utils.DateUtils;


@Namespaces(value = { @Namespace("/room") })
public class RM007AAction extends BaseAction {

	private static final Logger logger = Logger.getLogger(RM007AAction.class);
	private RoomProfileService roomProfileService = lookupBean(RoomProfileService.class, "RoomProfileService");
	private RMrmlstModel model;
	private String rlrmid;
	
	private String rowntfsdat;
	private String rtnttfsdat;
	
	@Action(value = "/RM007A", results = { @Result(name = "success", location = "RM007A.tiles", type = "tiles") })
	public String execute() throws Exception {
		logger.info("## "+this.getClass().getSimpleName());
		
		if(null != rlrmid){
			model = roomProfileService.getRoomProfileById(Integer.parseInt(rlrmid));
			model.getOwnModel().getRowntfsdat();
			setRowntfsdat(DateUtils.getDateStringFormate(model.getOwnModel()!=null? model.getOwnModel().getRowntfsdat() : null));
			setRtnttfsdat(DateUtils.getDateStringFormate(model.getOwnModel()!=null? model.getTntModel().getRtnttfsdat() : null));
		    setModel(model);
		}

		return SUCCESS;
	}
	
	@Action(value = "/RM007Asave", results = { @Result(name = "success", location="RM007", type = "redirect" , params = {"msg.msg", "S001"}) })
	public String RM007Asave() throws Exception {
		Msg m = new Msg();
		logger.info("## "+this.getClass().getSimpleName());
		logger.info("RM007Asave  >>>> model: "+ToStringBuilder.reflectionToString(model));

		try{
//		if(model.getRlrmid() == null){
			model.getOwnModel().setRlrmid(model.getRlrmid());
			model.getTntModel().setRlrmid(model.getRlrmid());
			roomProfileService.update(model);
//		}else{
//			// edit
//			roomProfileService.update(model);
//		}
		
		}catch( Exception e){
			logger.info(" ERROR ID : " + e.getMessage());
			e.printStackTrace();
			m.setType(MsgType.ERROR );
			m.setMsg(e.getMessage()	);
			setMsg(m);
		}
		setMsg(m);
		return SUCCESS;
	}
	
	public RMrmlstModel getModel() {
		return model;
	}

	public void setModel(RMrmlstModel model) {
		this.model = model;
	}

	public String getRlrmid() {
		return rlrmid;
	}

	public void setRlrmid(String rlrmid) {
		this.rlrmid = rlrmid;
	}

	public String getRowntfsdat() {
		return rowntfsdat;
	}

	public void setRowntfsdat(String rowntfsdat) {
		this.rowntfsdat = rowntfsdat;
	}

	public String getRtnttfsdat() {
		return rtnttfsdat;
	}

	public void setRtnttfsdat(String rtnttfsdat) {
		this.rtnttfsdat = rtnttfsdat;
	}

	
}
